package com.cloud.pole.msg.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.msg.biz.MsgNoticeBiz;
import com.cloud.pole.msg.biz.SmsSendHisBiz;
import com.cloud.pole.msg.model.entity.MsgNotice;
import com.cloud.pole.msg.model.entity.MsgVerfyMsgcodeAdmin;
import com.cloud.pole.msg.service.MsgNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/*
*2019年3月15日
*cloud-msg
*author:frank-chen
*/

@Slf4j
@Service
public class MsgNoticeServiceImpl implements MsgNoticeService {
	
	@Autowired
	private MsgNoticeBiz msgNoticeBiz;

	@Override
	public List<MsgNotice> getSysNoticeList(Integer userType, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(1, pageSize); 
		List<MsgNotice> list = msgNoticeBiz.getSysNoticeList(userType);
    	PageInfo<MsgNotice> page = new PageInfo<MsgNotice>(list);
		return page.getList();
	}

	@Override
	public PageInfo<MsgNotice> getPageUserNoticeList(String token, Integer userType, Integer pageSize, Integer pageNum)
			throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		PageHelper.startPage(pageNum, pageSize); 
		Long userId = Long.parseLong(jwtInfo.getId());
    	List<MsgNotice> list = msgNoticeBiz.selectPageUserNoticeListByMap(userType, userId);
    	PageInfo<MsgNotice> page = new PageInfo<MsgNotice>(list);
    	return page;
	}

	@Override
	public MsgNotice getNotice(String token, Integer noticeId) throws Exception {
		// TODO Auto-generated method stub
		MsgNotice msgNotice = msgNoticeBiz.selectByPrimaryKey(noticeId);
		if(msgNotice == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "通知不存在");
		}
		if(msgNotice.getCheckStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "通知无效");
		}
		if(msgNotice.getNoticeStatus() != 1) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "通知无效");
		}
		//如果高级需要做用户权限控制
		Long userId = 0L;
		if(!StringUtils.isEmpty(token)) {
			IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		    userId = Long.parseLong(jwtInfo.getId());
		}
		if(msgNotice.getUserType() == MsgNotice.CONTENT_TYPE_PERSON) {
			if(StringUtils.isEmpty(token)) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "请先登录");
			}
		}
		return msgNotice;
	}
}
